package com.panshi.controller.home;

import com.panshi.api.IMessagApi;
import com.panshi.api.UserGetApi;
import com.panshi.api.ZxApi;
import com.panshi.utils.GetSessionUser;
import com.panshi.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Controller
public class ZxController {

    @Autowired
    ZxApi zxApi;


    @Autowired
    UserGetApi userGetApi;

    //资讯
    @GetMapping(value = {"/zx/{type}", "/zx"})
    public String news(@PathVariable(required = false) String type, Map<String, Object> model) {
        int userId = GetSessionUser.getSessionUserId();
        model.put("userId", userId);
        model.put("loginUser", userGetApi.findUserByUid(userId + ""));
        if (null == type) {
            model.put("now", "1");
        } else if (type.equals("2")) {
            model.put("now", "2");
        } else if (type.equals("3")) {
            model.put("now", "3");
        } else {
            return null;
        }
        model.put("expert", zxApi.listExpertRecommendArticleOkVOs());
        model.put("titlelist", zxApi.listTitleVOs());
        model.put("allhere", zxApi.listAllWatchArticleOkVOs());
        return "home_page/news";
    }

    //资讯 加载更多
    @CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping("/zx/load/{type}/{page}")
    public String loadZx(@PathVariable(value = "type") Integer type, @PathVariable(value = "page") Integer page, Map<String, Object> model) {
        int userId = GetSessionUser.getSessionUserId();
        model.put("userId", userId);
        model.put("loginUser", userGetApi.findUserByUid(userId + ""));
        model.put("titlelist", zxApi.listTitleVOs());
        model.put("zxList", zxApi.listSortLoadArticleOkVOs(type, page, -1));
        return "home_page/load";
    }


    //相关推荐 加载更多
    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping("/recommend/{page}/{sort}")
    public String listRecommendArticleOkVOs(@PathVariable(value = "page") Integer page, @PathVariable(value = "sort") String sort, Map<String, Object> model) {
        int userId = GetSessionUser.getSessionUserId();
        model.put("userId", userId);
        model.put("loginUser", userGetApi.findUserByUid(userId + ""));
        model.put("titlelist", zxApi.listTitleVOs());
        model.put("zxList", zxApi.listRecommendArticleOkVOs(page * 10 - 1 + "", sort));
        return "home_page/load";
    }

    //分类
    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping(value = {"/zx/sort/{type}/{id}", "/zx/sort/{id}"})
    public String laydt(@PathVariable(required = false) Integer type, @PathVariable(value = "id") Integer id, Map<String, Object> model) {
        int userId = GetSessionUser.getSessionUserId();
        model.put("userId", userId);
        model.put("loginUser", userGetApi.findUserByUid(userId + ""));
        model.put("title", id);
        model.put("titlelist", zxApi.listTitleVOs());
        model.put("other", zxApi.listRelatedRecommendArticleOkVOs(id));
        model.put("otlook", zxApi.listOtherArticleOkVOs());
        if (null == type) {
            model.put("now", "1");
        } else {
            model.put("now", type);
        }
        return "home_page/sort";
    }

    //分类 加载新闻
    @CrossOrigin(origins = "*", maxAge = 3600)
    @PostMapping("/sort/load/{type}/{page}/{sort}")
    public String loadall(@PathVariable(value = "type") Integer type, @PathVariable(value = "page") Integer page, @PathVariable(value = "sort") int sort, Map<String, Object> model) {
        int userId = GetSessionUser.getSessionUserId();
        model.put("userId", userId);
        model.put("loginUser", userGetApi.findUserByUid(userId + ""));
        model.put("titlelist", zxApi.listTitleVOs());
        model.put("zxList", zxApi.listSortLoadArticleOkVOs(type, page, sort));
        return "home_page/load";
    }


    //查询 推荐页数
    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping("/page/{sort}")
    @ResponseBody
    public int countRecommendPage(@PathVariable(value = "sort") Integer sort) {
        return zxApi.countRecommendPage(sort);
    }

    //查询 推荐页数
    @CrossOrigin(origins = "*", maxAge = 3600)
    @GetMapping("/zx/page")
    @ResponseBody
    public int countNewsPage() {
        return zxApi.countNewsPage();
    }
}